﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BUS;
namespace AirPlane
{
    public partial class ajaxPaging : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (Page.IsPostBack)
            {
                ProcessForm();
            }
            else
            {
             
                int page = 1;


                if (Request["page"] != null)
                    page = int.Parse(Request["page"].ToString());
                Response.Clear();						//clears the existing HTML
                Response.ContentType = "text/plain";	//change content type
                if (!IsPostBack && Request["loai"] != null && Request["con"] != null && Request["state"] != null)
                {
                    if (Request["state"].ToString() == "")
                        Response.Write(hienThiKetQuaTimKiemAJax2(Request["loai"].ToString(),Request["con"].ToString(), page));
                    else
                        Response.Write(hienThiKetQuaTimKiemAJax1(Request["loai"].ToString(), bool.Parse(Request["state"].ToString()), Request["con"].ToString(), page));
                }
                Response.End();
                
                
            }

        }
        private void ProcessForm()
        {
            //end
        }
        public string hienThiKetQuaTimKiemAJax1(string loaiThacMac, bool trangThai, string dieuKien, int trangThu)
        {
            DataTable dt = new DataTable();
             DataTable dt1 = new DataTable();
            GopYBUS bus = new GopYBUS();
            int kichThuocTrang = 1;
            string loai = loaiThacMac;
            

            dt = bus.layThongTinGopY(loaiThacMac, trangThai, dieuKien, kichThuocTrang, trangThu);

            string row = "";
            int i = 0;
            int sodong = 0;
            int numrow = 0;
            dt1 = bus.laySoDongGopY(loaiThacMac,trangThai,dieuKien);
            numrow = dt1.Rows.Count;
            foreach (DataRow dr1 in dt1.Rows)
            {

                if ((dr1["loaiYeuCau"].ToString().Equals("Khen Ngợi Động Viên") || dr1["loaiYeuCau"].ToString().Equals("Phàn Nàn") || dr1["loaiYeuCau"].ToString().Equals("Khác")) && Session["type"].ToString() == "1")
                {
                    numrow -= 1;
                }
            }

            if (dt.Rows.Count == 0)
            {
                row += "<p style= 'color:Red '>Không tìm thấy!</p>";

            }
            else
            {
                row += "<table id ='table-b1'>";
                row += "<tr >";
                row += "<th width= '80px'>Loại Góp Ý</th>";
                row += "<th width= '300px'>Nội Dung</th>";
                row += "<th width= '100px'>Khách Hàng </th>";
                row += "<th width= '80px'>Ngày Góp Ý </th>";
                row += "<th width= '80px'>Tình Trạng </th>";


                row += "</tr>";
                foreach (DataRow dr in dt.Rows)
                {
                    string loaiGopY = dr["loaiYeuCau"].ToString();
                    string noiDung = dr["noiDung"].ToString();
                    string hoTen = dr["hoTenKhachHang"].ToString();
                    string ngayGopY = dr["ngayGopY"].ToString();
                    string tinhTrang = dr["tinhTrang"].ToString();

                    if (loaiThacMac == "%%" && Session["type"].ToString() == "1")
                    {
                        if (loaiGopY.Equals("Khen Ngợi Động Viên") || loaiGopY.Equals("Phàn Nàn") || loaiGopY.Equals("Khác"))
                        {
                            continue;
                            
                        }
                    }
                    if (tinhTrang == "True")
                        tinhTrang = "Đã Trả Lời";
                    else
                        tinhTrang = "Chưa Trả Lời";
                    if (noiDung.Length <= 100)
                        noiDung = noiDung.Substring(0, noiDung.Length) + "...";
                    else
                        noiDung = noiDung.Substring(0, 100) + "...";
                    row += "<tr >";
                    row += "<td width= '80px'>" + loaiGopY + "</td>";
                    row += "<td width= '300px'>" + "<a class='tab-gopY' href='../ChiTietGopY.aspx?id=" + dr["ID_gopY"].ToString() + "'>" + noiDung + "</a></td>";
                    row += "<td width= '100px'>" + hoTen + "</td>";
                    row += "<td width= '80px'>" + ngayGopY + "</td>";
                    row += "<td width= '80px'>" + tinhTrang + "</td>";

                    row += "</tr>";
                }
                row += "</table>";
            }
            if(numrow > 0)
            {
            int start = (trangThu - 1) * kichThuocTrang;
            int prev = trangThu - 1;
            int next = trangThu + 1;
            row += "<div style='margin-left:50%'>";
            if (!(start <= 0))
                row += "<a href='javascript:void(0);' onClick='return searchcall_back(" + prev + ");' id='txtblue' > Prev </a> ";


            int x = 1;
            for (int j = 0; j < numrow; j = j + kichThuocTrang)
            {
                if (start != j)
                {
                    row += "<a href='javascript:void(0);' onClick='return searchcall_back(" + x + ");' id='txtblue' > " + x + " </a> ";
                }
                else
                {

                    row += "<b id='txtred' > " + x + " </b>";
                }
                x++;
            }

            if (!(start >= numrow - kichThuocTrang))
                row += "<a href='javascript:void(0);' onclick='return searchcall_back(" + next + ");' id='txtblue' > Next </a> ";
        }
            row += "</div>";

            return row;
        }
        public string hienThiKetQuaTimKiemAJax2(string loaiThacMac,string dieuKien, int trangThu)
        {
            DataTable dt = new DataTable();
            DataTable dt1 = new DataTable();
            GopYBUS bus = new GopYBUS();
            int kichThuocTrang = 1;
            string loai = loaiThacMac;


            dt = bus.layThongTinGopY(loaiThacMac,dieuKien , kichThuocTrang, trangThu);

            string row = "";
            int i = 0;
            int sodong = 0;

            int numrow = 0;
            dt1 = bus.laySoDongGopY(loaiThacMac,dieuKien);
            numrow = dt1.Rows.Count;

            foreach (DataRow dr1 in dt1.Rows)
            {

                if ((dr1["loaiYeuCau"].ToString().Equals("Khen Ngợi Động Viên") || dr1["loaiYeuCau"].ToString().Equals("Phàn Nàn") || dr1["loaiYeuCau"].ToString().Equals("Khác")) && Session["type"].ToString() == "1")
                {
                    numrow -= 1;
                }
            }


            if (dt.Rows.Count == 0)
            {
                row += "<p style= 'color:Red '>Không tìm thấy!</p>";

            }
            else
            {
                row += "<table id ='table-b1'>";
                row += "<tr >";
                row += "<th width= '80px'>Loại Góp Ý</th>";
                row += "<th width= '300px'>Nội Dung</th>";
                row += "<th width= '100px'>Khách Hàng </th>";
                row += "<th width= '80px'>Ngày Góp Ý </th>";
                row += "<th width= '80px'>Tình Trạng </th>";


                row += "</tr>";
                foreach (DataRow dr in dt.Rows)
                {
                    string loaiGopY = dr["loaiYeuCau"].ToString();
                    string noiDung = dr["noiDung"].ToString();
                    string hoTen = dr["hoTenKhachHang"].ToString();
                    string ngayGopY = dr["ngayGopY"].ToString();
                    string tinhTrang = dr["tinhTrang"].ToString();

                    if (loaiThacMac == "%%" && Session["type"].ToString() == "1")
                    {
                        if (loaiGopY.Equals("Khen Ngợi Động Viên") || loaiGopY.Equals("Phàn Nàn") || loaiGopY.Equals("Khác"))
                        {
                            continue;
                           
                        }
                    }
                    if (tinhTrang == "True")
                        tinhTrang = "Đã Trả Lời";
                    else
                        tinhTrang = "Chưa Trả Lời";
                    if (noiDung.Length <= 100)
                        noiDung = noiDung.Substring(0, noiDung.Length) + "...";
                    else
                        noiDung = noiDung.Substring(0, 100) + "...";
                    row += "<tr >";
                    row += "<td width= '80px'>" + loaiGopY + "</td>";
                    row += "<td width= '300px'>" + "<a class='tab-gopY' href='../ChiTietGopY.aspx?id=" + dr["ID_gopY"].ToString() + "'>" + noiDung + "</a></td>";
                    row += "<td width= '100px'>" + hoTen + "</td>";
                    row += "<td width= '80px'>" + ngayGopY + "</td>";
                    row += "<td width= '80px'>" + tinhTrang + "</td>";

                    row += "</tr>";
                }
                row += "</table>";
            }
            if (numrow > 0)
            {
                int start = (trangThu - 1) * kichThuocTrang;
                int prev = trangThu - 1;
                int next = trangThu + 1;
                row += "<div style='margin-left:50%'>";
                if (!(start <= 0))
                    row += "<a href='javascript:void(0);' onClick='return searchcall_back(" + prev + ");' id='txtblue' > Prev </a> ";


                int x = 1;
                for (int j = 0; j < numrow; j = j + kichThuocTrang)
                {
                    if (start != j)
                    {
                        row += "<a href='javascript:void(0);' onClick='return searchcall_back(" + x + ");' id='txtblue' > " + x + " </a> ";
                    }
                    else
                    {

                        row += "<b id='txtred' > " + x + " </b>";
                    }
                    x++;
                }

                if (!(start >= numrow - kichThuocTrang))
                    row += "<a href='javascript:void(0);' onclick='return searchcall_back(" + next + ");' id='txtblue' > Next </a> ";
            }
            row += "</div>";

            return row;
        }
    }
}